# This is a simple test, 

# Start the server before running this
#
# Start the server from this directory with python ../launch.py
# otherwise it cannot read the test configuration
#
#
import time
import os
import subprocess
from mediadart.storage import Storage
from mediadart.mqueue.mqclient_async import Proxy


def show_output(status_ok, result, userargs ):
    if status_ok:
        print
        print
        print '**** Feature Extraction: algoritmo %s ******' % userargs
        print result
    else:
        print
        print
        print '**** ERROR in Feature Extraction: algoritmo %s ******' % userargs
        print result


def show_output2(status_ok, result, userargs ):
    if status_ok:
        print
        print
        print '**** Adaptation of %s: result in %s' % (userargs, result)
    else:
        print
        print
        print '**** ERROR in adaptation of %s:  %s' % (userargs, str(result))

subprocess.call(['/bin/rm', '-rf', './cache'])
os.mkdir('./cache')
fc  = Storage('./cache')
mp3 = fc.add('./mp3test.mp3')
print 'mp3', mp3
avi = fc.add('./avitest.avi')
print 'avi', avi
gif = fc.add('./giftest.gif')
print 'gif', gif
mov = fc.add('./movtest.mov')
print 'mov', mov
jpg = fc.add('./jpgtest.jpg')
print 'jpg', jpg
pdf = fc.add('./pdftest.pdf')
print 'pdf', pdf


extractor = Proxy('FeatureExtractor', show_output)
adapter = Proxy('Adapter')
print 'pinging adapter', adapter.ping()

print 
print 
print 'adapting pdf: slow, so execute asynchronously'
Proxy('Adapter', show_output2, pdf).adapt_doc(pdf, 'pdf', 15000)


print
print
print 'adapting to flv'
id3 = adapter.adapt_video2(avi, 'flv')
print 'flv saved in %s' % id3



print
print 
print 'adapting mp3'
id2 = adapter.adapt_audio2(mp3, 'aac', {})
print 'result is %s' % id2

print 
print 'extracting thumbnail'
id4 = adapter.extract_video_thumbnail(avi, 256, 256, 50)
print 'thumbnail saved in  %s' % id4

Proxy('FeatureExtractor', show_output, 'audio_basic').extract(mp3, 'audio_basic')
Proxy('FeatureExtractor', show_output, 'doc_basic').extract(pdf, 'doc_basic')
Proxy('FeatureExtractor', show_output, 'image_basic').extract(gif, 'image_basic')
Proxy('FeatureExtractor', show_output, 'image_histogram').extract(gif, 'image_histogram')
Proxy('FeatureExtractor', show_output, 'video_basic').extract(avi, 'video_basic')
Proxy('FeatureExtractor', show_output, 'video_features_extractor').extract(mov, 'video_features_extractor')
#Proxy('FeatureExtractor', show_output, userargs).extract(jpg, 'xmp_extractor')
Proxy('FeatureExtractor', show_output, 'image_basic image_histogram').extract_features(gif, ['image_basic', 'image_histogram'])

